require'love.thread'
require'love.filesystem'
require'love.event'
require'love.timer'
require'love.graphics'
require'love.image'
class = require'lib.middleclass'
Stateful = require'lib.stateful'
JSON = require'lib.JSON'
--loader = require'lib.ATL.trunk.loader'
require'enet'
require'src.server'
require'src.guest'

_print = print

function print(...)

	local s = {...}

	for i = 1, #s do
		if s[i] == true then s[i] = "true"
		elseif s[i] == false then s[i] = "false" 
		elseif not s[i] then s[i] = "nil" end

		if type(s[i]) == "userdata" then
			s[i] = "userdata"
		end
		
		_print(os.date("%H:%M:%S")..": "..s[i])
		
		if love.filesystem.exists('log') then
			love.filesystem.append('log', os.date("%H:%M:%S")..": ".. s[i]..'\r\n')
		else
			love.filesystem.write('log', os.date("%H:%M:%S")..": ".. s[i]..'\r\n')
		end
	end
end


function love.load()
	print("love loaded")
	--loader.path = ("assets/maps/")
	server = Server:new()
	love.filesystem.setIdentity('infopunk-server')
	print(love.filesystem.getSaveDirectory())
	constants = {}
	constants.training_points = 60
	constants.character_points = 380
	stat_chart = {
		["energy"] = {
			["cost"] = 1.5,
			["compute"] = function(guest) return ((guest.stats.might+guest.stats.persistence)/2) end,
		},
		["grit"] = {
			["cost"] = 1.5,
			["compute"] = function(guest) return ((guest.stats.coordination+guest.stats.lithe)/2) end,
		},
		["concentration"] = {
			["cost"] = 2,
			["compute"] = function(guest) return ((guest.stats.understanding+guest.stats.resolve)/4) end,
		},
		["might"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.might end,
		},
		["persistence"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.persistence end,
		},
		["coordination"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.coordination end,
		},
		["lithe"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.lithe end,
		},
		["understanding"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.understanding end,
		},
		["resolve"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.resolve end,
		},
		["kismet"] = {
			["cost"] = 4,
			["compute"] = function (guest) return guest.stats.kismet end,
		},
		["run"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/2) end,
		},
		["jump"] = {
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/3) end,
		},
		["light_melee"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/2) end,
		},
		["heavy_melee"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.might)/2) end,
		},
		["light_ranged"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/2) end,
		},
		["heavy_ranged"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.might)/2) end,
		},
		["akimbo"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination)/3) end,
		},
		["stealth"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/4) end,
		},
		["detect_stealth"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.understanding)/4) end,
		},
		["locksmithing"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/3) end,
		},
		["tracking"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.lithe)/2) end,
		},
		["trapping"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.lithe)/2) end,
		},
		["melee_d"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.persistence + guest.stats.might)/4) end,
		},
		["ranged_d"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.persistence)/3) end,
		},
		["kidnapping"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.might + guest.stats.resolve)/4) end,
		},
		["interrogation"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.persistence)/4) end,
		},
		["escape"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.coordination + guest.stats.lithe)/3) end,
		},
		["tolerance"]	={
			["cost"] = 1,
			["compute"] = function (guest) return ((guest.stats.resolve + guest.stats.persistence)/3) end,
		},
		["elementalism"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["chaos"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["blessing"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["summoning"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["mentalism"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["demolitions"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["medicine"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["mechanics"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["cyber"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		["psychology"]	={
			["cost"] = 2,
			["compute"] = function (guest) return ((guest.stats.understanding + guest.stats.resolve)/4) end,
		},
		
	}
	skill_cost_list = {
		["run"] = { ["train"] = 2, ["spec"] = 4 },
		["jump"] = { ["train"] = 2, ["spec"] = 6 },
		["light_melee"] = { ["train"] = 4, ["spec"] = 8 },
		["heavy_melee"] = { ["train"] = 8, ["spec"] = 12 },
		["light_ranged"] = { ["train"] = 4, ["spec"] = 6 },
		["heavy_ranged"] = { ["train"] = 8, ["spec"] = 14 },
		["akimbo"] = { ["train"] = 4, ["spec"] = 6 },
		["stealth"] = { ["train"] = 12, ["spec"] = 18 },
		["detect_stealth"] = { ["train"] = 12, ["spec"] = 14 },
		["locksmithing"] = { ["train"] = 6, ["spec"] = 12 },
		["tracking"] = { ["train"] = 4, ["spec"] = 8 },
		["trapping"] = { ["train"] = 6, ["spec"] = 12 },
		["melee_d"] = { ["train"] = 8, ["spec"] = 14 },
		["ranged_d"] = { ["train"] = 6, ["spec"] = 10 },
		["kidnapping"] = { ["train"] = 12, ["spec"] = 16 },
		["interrogation"] = { ["train"] = 10, ["spec"] = 14 },
		["escape"] = { ["train"] = 4, ["spec"] = 8 },
		["tolerance"] = { ["train"] = 8, ["spec"] = 12 },
		["elementalism"] = { ["train"] = 16, ["spec"] = 16 },
		["chaos"] = { ["train"] = 4, ["spec"] = 8 },
		["blessing"] = { ["train"] = 4, ["spec"] = 8 },
		["summoning"] = { ["train"] = 4, ["spec"] = 8 },
		["mentalism"] = { ["train"] = 4, ["spec"] = 8 },
		["demolitions"] = { ["train"] = 4, ["spec"] = 8 },
		["medicine"] = { ["train"] = 4, ["spec"] = 8 },
		["mechanics"] = { ["train"] = 4, ["spec"] = 8 },
		["cyber"] = { ["train"] = 4, ["spec"] = 8 },
		["psychology"] = { ["train"] = 4, ["spec"] = 8 },

	}

end

function love.update(dt)
	dt = math.min(0.01666667, dt)
	server:update(dt)
end

function love.draw()
	server:draw()
end

function love.keypressed(key,isRepeat)
	server:keypressed(key, isRepeat)
end

function love.keyreleased(key)
	server:keyreleased(key)
end

function love.mousepressed(x, y, button)
	server:mousepressed(x, y, button)
end

function love.mousereleased(x, y, button)
	server:mousereleased(x, y, button)
end

function love.threaderror(thread, errorstr)
	print("Thread Error!\n" .. errorstr)
end

function love.quit()
	server:quit()
end

function table.val_to_str ( v )
  if "string" == type( v ) then
    v = string.gsub( v, "\n", "\\n" )
    if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
      return "'" .. v .. "'"
    end
    return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  else
    return "table" == type( v ) and table.tostring( v ) or
      tostring( v )
  end
end

function table.key_to_str ( k )
  if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
    return k
  else
    return "[" .. table.val_to_str( k ) .. "]"
  end
end

function table.tostring( tbl )
  local result, done = {}, {}
  for k, v in ipairs( tbl ) do
    table.insert( result, table.val_to_str( v ) )
    done[ k ] = true
  end
  for k, v in pairs( tbl ) do
    if not done[ k ] then
      table.insert( result,
        table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
    end
  end
  return "{" .. table.concat( result, "," ) .. "}"
end

local running = true
love.load()
while running do
	love.update(love.timer.getDelta())
end


